package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.mapper.EventMapper;
import com.example.mapper.RegistrationMapper;
import com.example.mapper.UserMapper;
import com.example.pojo.Event;
import com.example.pojo.EventDetailVO;
import com.example.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EventServiceImpl implements EventService {
    @Autowired
    private EventMapper eventMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public EventDetailVO getEvent(Integer id) {
        try {
            EventDetailVO event = eventMapper.detailWithSpot(id);
            List<String> usernameList = userMapper.getUserList(id);
            if(usernameList.isEmpty()) return event;
            event.setUserStr(String.join(",", usernameList));
            return event;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Event> getEvents(Integer spotId) {
        try {
            LambdaQueryWrapper<Event> eq = new LambdaQueryWrapper<Event>()
                    .eq(spotId != null, Event::getSpotId, spotId);
            return eventMapper.selectList(eq);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
